package org.buptsse.utils;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;

public class Upload extends Activity {
    private File file;
    private Handler handler;
    private static final int MAXSIZE=1024*1024*10;
    /**
     * 上传图片到服务器
     * 
     * @param imageFile 包含路径
     */
    public String uploadFile(String uploadUrl,String filePath,int postId,int userId) {
    	int fileLength;
        try {
            String requestUrl = uploadUrl;
            //请求普通信息
            File file=new File(filePath);
			if(!file.exists())
			{
				System.out.println("file is not exist!!");
				return "fail";
			}
			int point=filePath.lastIndexOf('.');
			String FileType=filePath.substring(point+1,filePath.length());
			fileLength=(int) file.length();
			if(fileLength>MAXSIZE){System.out.println("file is too large!");return "fail";}
			JSONObject obj=new JSONObject();
			obj.put("postid",postId);
			obj.put("userid",userId);
			obj.put("filetype",FileType);
            Map<String, String> params = new HashMap<String, String>();
            params.put("json", obj.toString());
            //上传文件
            FormFile formfile = new FormFile(file.getName(), file, "image", "application/octet-stream");
            
            SocketHttpRequester.post(requestUrl, params, formfile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "success";
    }
}